// StandardGenerator.cpp: implementation of the StandardGenerator class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PHSShare.h"
#include "StandardGenerator.h"
#include "DBConnectionManager.h"
#include "PartSort.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

using namespace JointSystem;
using namespace DAWrapper;

StandardGenerator::StandardGenerator()
{

}

StandardGenerator::~StandardGenerator()
{

}

Standard* StandardGenerator::Generate(ProductCatalog* pCatalog)
{
	ASSERT(pCatalog);
	Standard* pStandard = NULL;

	pStandard = this->GetManaged(pCatalog->ID());
	if(pStandard != NULL)
		return pStandard;

	pStandard = GenerateNew(pCatalog);
	if(pStandard == NULL)
		return NULL;

	this->Add(pCatalog->ID(), pStandard);
	pStandard->Catalog( pCatalog );

	return pStandard;
}

Standard* StandardGenerator::GenerateNew(ProductCatalog* pCatalog)
{
	Standard* pStandard = NULL;

	switch(pCatalog->Style())
	{
	case ePA:
		pStandard = &InitStandard(*(new StandardOfClamp), pCatalog);
		break;
	case eSA:
		pStandard = &InitStandard(*(new StandardOfSA), pCatalog);
		break;
	case eCONNECT:
		pStandard = &InitStandard(*(new StandardOfConnect), pCatalog);
		break;
	case eSPRING:
		pStandard = &InitStandard(*(new StandardOfSpring), pCatalog);
		break;
	case eCONSTSPRING:
		pStandard = &InitStandard(*(new StandardOfConstSpring), pCatalog);
		break;
	case eBOLTNUT:
		pStandard = &InitStandard(*(new StandardOfBoltNut), pCatalog);
		break;
	case eATTACH:
		pStandard = &InitStandard(*(new StandardOfAttach), pCatalog);
		break;
	case eSTEEL:
		pStandard = &InitStandard(*(new StandardOfProfiledBar), pCatalog);
		break;
	case ePIPE:
		pStandard = &InitStandard(*(new StandardOfProfiledBar), pCatalog);
		break;
	default:
		ASSERT(FALSE);
	}
	if(pStandard == NULL)
	{
		ReportMemoryError();
		return NULL;
	}
	pStandard->SetStyle(pCatalog->Style());
	return pStandard;
}

BOOL StandardGenerator::GetStandardDataSet(Standard& standard, _RecordsetPtr& rs)
{	
	ASSERT(!standard.GetInfoTable().IsEmpty());
	ASSERT(!standard.GetStandard().IsEmpty());
	CString strSql;
	strSql.Format(_T("select * from %s where standard = '%s'"),
		standard.GetInfoTable(), standard.GetStandard());
	
	const _ConnectionPtr& pConn = *DBConnectionManager::Instance()->GetConnectionPtrByUri(
		standard.GetDBSourceURL());
	rs = pConn->Execute(_bstr_t(strSql), NULL, adCmdText);
	
	if(rs == NULL)
		return FALSE;
	
	if(!(rs->adoEOF && rs->BOF))
	{
		return TRUE;
	}
	else
	{
		ASSERT(FALSE);
		return FALSE;
	}
	
	rs->Close();
	return TRUE;
}


void StandardGenerator::ReadDataForObject(const _RecordsetPtr& rs, StandardOfClamp& standard)
{
	ReadData(rs, standard);
	standard.SetCrudeDataTable(vtos(rs->GetCollect(_T("CrudePA"))));
	standard.SetDescription(vtos(rs->GetCollect(_T("StandardDesc"))));
	standard.SetFixDataTable(vtos(rs->GetCollect(_T("CrudePAfix"))));
	standard.SetFolder(vtos(rs->GetCollect(_T("Folder"))));
	standard.SetIDDataTable(vtos(rs->GetCollect(_T("tbnPAid"))));
	standard.SetObservation(vtos(rs->GetCollect(_T("Observation"))));
	standard.SetStandardName(vtos(rs->GetCollect(_T("Standard"))));
	standard.SetManufactory(vtos(rs->GetCollect(_T("manufactory"))));
	standard.SetStandardID(vtos(rs->GetCollect(_T("StandardID"))));
}

void StandardGenerator::ReadDataForObject(const _RecordsetPtr& rs, StandardOfSA& standard)
{
	ReadData(rs, standard);
	standard.SetStandardName(vtos(rs->GetCollect(_T("Standard"))));
	standard.SetCrudeDataTable(vtos(rs->GetCollect(_T("crudeSA"))));
	standard.SetIDDataTable(vtos(rs->GetCollect(_T("tbnSAID"))));
	standard.SetProfiledBarCrudeDataTable(vtos(rs->GetCollect(_T("crudesectionsteel"))));
	standard.SetProfiledBarIDDataTable(vtos(rs->GetCollect(_T("crudesectionsteelID"))));
	standard.SetBoltNutCrudeDataTable(vtos(rs->GetCollect(_T("crudeSAboltsnuts"))));
	standard.SetBoltNutIDDataTable(vtos(rs->GetCollect(_T("crudesaboltsnutsid"))));
	standard.SetAttachCrudeDataTable(vtos(rs->GetCollect(_T("crudeAttachment"))));
	standard.SetAttachIDDataTable(vtos(rs->GetCollect(_T("crudeAttachmentID"))));
	standard.SetObservation(vtos(rs->GetCollect(_T("observation"))));
	standard.SetStandardID(vtos(rs->GetCollect(_T("standardid"))));
	standard.SetManufactory(vtos(rs->GetCollect(_T("manufactory"))));
	standard.SetFolder(vtos(rs->GetCollect(_T("folder"))));
	standard.SetDescription(vtos(rs->GetCollect(_T("Standarddesc"))));
}

void StandardGenerator::ReadDataForObject(const _RecordsetPtr& rs, StandardOfConnect& standard)
{
	ReadData(rs, standard);
	standard.SetStandardName(vtos(rs->GetCollect(_T("Standard"))));
	standard.SetCrudeDataTable(vtos(rs->GetCollect(_T("crudePart"))));
	standard.SetIDDataTable(vtos(rs->GetCollect(_T("tbnpartID"))));
	standard.SetLengthCaculateTable(vtos(rs->GetCollect(_T("luglencal"))));
	standard.SetRodInfoTable(vtos(rs->GetCollect(_T("luginfo"))));
	standard.SetObservation(vtos(rs->GetCollect(_T("observation"))));
	standard.SetStandardID(vtos(rs->GetCollect(_T("standardid"))));
	standard.SetManufactory(vtos(rs->GetCollect(_T("manufactory"))));
	standard.SetFolder(vtos(rs->GetCollect(_T("folder"))));
	standard.SetDescription(vtos(rs->GetCollect(_T("Standarddesc"))));
}

void StandardGenerator::ReadDataForObject(const _RecordsetPtr& rs, StandardOfConstSpring& standard)
{
	ReadData(rs, standard);
	standard.SetStandardName(vtos(rs->GetCollect(_T("Standard"))));
	standard.SetCrudeDataTable(vtos(rs->GetCollect(_T("Spring_crude"))));
	standard.SetIDDataTable(vtos(rs->GetCollect(_T("Spring_id"))));
	standard.SetPropertyDataTable(vtos(rs->GetCollect(_T("Spring_property"))));
	standard.SetDiameterOfConstSpringTable(vtos(rs->GetCollect(_T("lugdiaofcspr"))));
	standard.SetObservation(vtos(rs->GetCollect(_T("observation"))));
	standard.SetStandardID(vtos(rs->GetCollect(_T("standardid"))));
	standard.SetManufactory(vtos(rs->GetCollect(_T("manufactory"))));
	standard.SetFolder(vtos(rs->GetCollect(_T("folder"))));
	standard.SetDescription(vtos(rs->GetCollect(_T("Standarddesc"))));
	standard.SetDisplacementSerierTable(vtos(rs->GetCollect(_T("DisplacementSerial"))));
	standard.SetDisplacementUnit(vtos(rs->GetCollect(_T("Unit_Displacement"))));
	standard.SetForceUnit(vtos(rs->GetCollect(_T("Unit_force"))));
	standard.SetMinSurplusDisplacement(vtos(rs->GetCollect(_T("minsurplusdist"))));
	standard.SetNutDiameterSerierTable(vtos(rs->GetCollect(_T("DiameterSerial"))));
	standard.SetBoltNutRodSelectTable(vtos(rs->GetCollect(_T("spring_boltsnutslugsel"))));
	standard.SetSurplusDisplacementPercent(vtos(rs->GetCollect(_T("percentsurplusdist"))));
}

void StandardGenerator::ReadDataForObject(const _RecordsetPtr& rs, StandardOfSpring& standard)
{
	ReadData(rs, standard);
	standard.SetStandardName(vtos(rs->GetCollect(_T("Standard"))));
	standard.SetCrudeDataTable(vtos(rs->GetCollect(_T("Spring_crude"))));
	standard.SetPropertyDataTable(vtos(rs->GetCollect(_T("Spring_property"))));
	standard.SetIDDataTable(vtos(rs->GetCollect(_T("Spring_id"))));
	standard.SetObservation(vtos(rs->GetCollect(_T("observation"))));
	standard.SetStandardID(vtos(rs->GetCollect(_T("standardid"))));
	standard.SetManufactory(vtos(rs->GetCollect(_T("manufactory"))));
	standard.SetFolder(vtos(rs->GetCollect(_T("folder"))));
	standard.SetDescription(vtos(rs->GetCollect(_T("Standarddesc"))));
	standard.SetDisplacementUnit(vtos(rs->GetCollect(_T("Unit_Displacement"))));
	standard.SetForceUnit(vtos(rs->GetCollect(_T("Unit_force"))));
	standard.SetNutDiameterSerierTable(vtos(rs->GetCollect(_T("DiameterSerial"))));
	standard.SetMaxDisplacementTable(vtos(rs->GetCollect(_T("Spring_propertyMaxDist"))));
}

void StandardGenerator::ReadDataForObject(const _RecordsetPtr& rs, StandardOfBoltNut& standard)
{
	ReadData(rs, standard);
	standard.SetStandardName(vtos(rs->GetCollect(_T("Standard"))));
	standard.SetObservation(vtos(rs->GetCollect(_T("observation"))));
	standard.SetStandardID(vtos(rs->GetCollect(_T("standardid"))));
	standard.SetManufactory(vtos(rs->GetCollect(_T("manufactory"))));
	standard.SetFolder(vtos(rs->GetCollect(_T("folder"))));
	standard.SetDescription(vtos(rs->GetCollect(_T("Standarddesc"))));
	standard.SetCrudeDataTable(vtos(rs->GetCollect(_T("crudeboltsnuts"))));
	standard.SetIDDataTable(vtos(rs->GetCollect(_T("crudeboltsnutsid"))));
	standard.SetSurplusDataTable(vtos(rs->GetCollect(_T("boltssurpluslength"))));
}

void StandardGenerator::ReadDataForObject(const _RecordsetPtr& rs, StandardOfAttach& standard)
{
	ReadData(rs, standard);
	standard.SetStandardName(vtos(rs->GetCollect(_T("Standard"))));
	standard.SetCrudeDataTable(vtos(rs->GetCollect(_T("crudeTable"))));
	standard.SetIDDataTable(vtos(rs->GetCollect(_T("idTable"))));
}

void StandardGenerator::ReadDataForObject(const _RecordsetPtr& rs, StandardOfProfiledBar& standard)
{
	ReadData(rs, standard);
	standard.SetStandardName(vtos(rs->GetCollect(_T("Standard"))));
	standard.SetCrudeDataTable(vtos(rs->GetCollect(_T("crudeTable"))));
	standard.SetIDDataTable(vtos(rs->GetCollect(_T("idTable"))));
}

void StandardGenerator::ReadData(const _RecordsetPtr& rs, Standard& standard)
{
	standard.ScriptTable(vtos(rs->GetCollect(_T("Script_Table"))));
	standard.PropertyTable(vtos(rs->GetCollect(_T("Property_Table"))));
	standard.FunctionTable(vtos(rs->GetCollect(_T("Function_Table"))));
	standard.JointPortTable(vtos(rs->GetCollect(_T("JointPort_Table"))));
	standard.JointPortPropertyTable(vtos(rs->GetCollect(_T("JointPort_Property_Table"))));
}